package com.chenjun.core.service.sys.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.core.dao.generator.entity.SysUserRole;
import com.chenjun.core.dao.generator.mapper.SysUserRoleMapper;
import com.chenjun.core.dao.module.SysUserRoleRelationMapper;
import com.chenjun.core.service.sys.SysUserRoleService;
import com.chenjun.core.vo.sys.UserRoleBean;
@SuppressWarnings("serial")
@Service
@Transactional
public class SysUserRoleServiceImpl implements SysUserRoleService{

	private static final String USER_ROLE_MAPPER = "UserRoleMapper";
    private static final Logger logger = Logger.getLogger(SysUserRoleServiceImpl.class);

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    
    @Autowired
    private SysUserRoleRelationMapper sysUserRoleRelationMapper;

	public SysUserRole get(String id){
		return sysUserRoleMapper.selectByPrimaryKey(id);
	}

	public int create(SysUserRole record){
		return sysUserRoleMapper.insert(record);
	}

	public int delete(String id){
		return sysUserRoleMapper.deleteByPrimaryKey(id);
	}
	
	public int deleteByCondition(Map<String,String> params){
		return sysUserRoleRelationMapper.deleteByCondition(params);
	}

	public int update(SysUserRole record){
		return sysUserRoleMapper.updateByPrimaryKey(record);
	}

	public int createOrUpdate(SysUserRole record){
		int rows = 0;
		if (StringUtils.isBlank(record.getId())) {
			rows = sysUserRoleMapper.insert(record);
		} else {
			rows = sysUserRoleMapper.updateByPrimaryKeySelective(record);
		}
		return rows;
	}

	public int doCountByCondition(DataGridModel param){
		return sysUserRoleRelationMapper.countByCondition(param);
	}

	public List<SysUserRole> doListByCondition(DataGridModel param){
		return sysUserRoleRelationMapper.selectByCondition(param);
	}
	
	public List<UserRoleBean> dofListByCondition(DataGridModel param){
		return sysUserRoleRelationMapper.selectfByCondition(param);
	}

	public Pagination doPagination(DataGridModel param){
		Pagination vo = new Pagination();
		List<SysUserRole> list = sysUserRoleRelationMapper.selectByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = sysUserRoleRelationMapper.countByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}
	
	public Pagination doUserRolePagination(DataGridModel param) {
		Pagination vo = new Pagination();
		List<UserRoleBean> list = sysUserRoleRelationMapper.selectUserRoleByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = sysUserRoleRelationMapper.countUserRoleByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}
}
